﻿// Example header text. Can be configured in the options.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace vtech_frameWorks.DAO
{
    public class ClsBaseDbConnectionDAO
    {
        #region GetConnection
        public static SqlConnection GetConnection()
        {
            return new SqlConnection(ConfigurationManager.ConnectionStrings["dbVtechConnectionString"].ConnectionString.Trim());
        }

        public static SqlConnection GetConnection(string connectionString)
        {
            return new SqlConnection(connectionString);
        }

        #endregion

        #region OpenConnection
        public static void OpenConnection(ref SqlConnection conn)
        {
            if (conn == null)
            {
                conn = GetConnection();
            }
            if (conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (System.Exception exception)
                {
                    throw new System.Exception("Cannot open a connection", exception);
                }
            }
        }

        public static void OpenConnection(ref SqlConnection conn, string connectionString)
        {
            if (conn == null)
            {
                conn = GetConnection(connectionString);
            }
            if (conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (System.Exception exception)
                {
                    throw new System.Exception("Cannot open a connection", exception);
                }
            }
        }

        public static SqlConnection OpenConnection(SqlTransaction sqlTransaction)
        {
            SqlConnection connection;
            if (sqlTransaction != null)
            {
                if (sqlTransaction.Connection == null)
                {
                    throw new ArgumentNullException("sqlTransaction");
                }
                connection = sqlTransaction.Connection;
            }
            else
            {
                connection = GetConnection();
            }
            if (connection.State != ConnectionState.Open)
            {
                try
                {
                    connection.Open();
                }
                catch (Exception exception)
                {
                    throw new Exception("Cannot open a connection", exception);
                }
            }
            return connection;
        }

        public static SqlConnection OpenConnection(SqlTransaction sqlTransaction, string connectionString)
        {
            SqlConnection connection;
            if (sqlTransaction != null)
            {
                if (sqlTransaction.Connection == null)
                {
                    throw new ArgumentNullException("sqlTransaction");
                }
                connection = sqlTransaction.Connection;
            }
            else
            {
                connection = GetConnection(connectionString);
            }
            if (connection.State != ConnectionState.Open)
            {
                try
                {
                    connection.Open();
                }
                catch (Exception exception)
                {
                    throw new Exception("Cannot open a connection", exception);
                }
            }
            return connection;
        }
        #endregion
    }
}